package com.szpx.service.hhxservice.impl;

import com.alibaba.druid.util.StringUtils;
import com.szpx.entity.hhxentity.Account;
import com.szpx.mapper.hhxmapper.AccountMapper;
import com.szpx.service.hhxservice.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;


    @Override
    public List<Account> findAll() {
        return accountMapper.selectAll();
    }

    @Override
    public Account findById(Integer id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Account account) {
        accountMapper.insert(account);

    }

    @Override
    public int update(Account account) {
      return accountMapper.updateByPrimaryKeySelective(account);

    }

    @Override
    public int delete(Integer id) {
      return accountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Account> findList(Account account) {
        Example example = creatExample(account);
        //根据构建的条件查询数据
        return accountMapper.selectByExample(example);
    }

    public Example creatExample(Account account) {
        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        if (account != null) {
            //用户id
            if (!StringUtils.isEmpty(account.getId())) {
                criteria.andEqualTo("id", account.getId());
            }
            //用户名称
            if (!StringUtils.isEmpty(account.getName())) {
                criteria.andLike("name", "%" + account.getName() + "%");
            }
        }
        return example;
    }


}
